package com.example.chuqq.gpssigninapplication.model;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;

import com.example.chuqq.gpssigninapplication.MainService;
import com.example.chuqq.gpssigninapplication.WatcherService;

public class UserData {
    private static final String TAG = "UserData";

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    private String name;

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getFirst() {
        return first;
    }

    public void setFirst(String first) {
        this.first = first;
    }

    private String first;

    public String getLast() {
        return last;
    }

    public void setLast(String last) {
        this.last = last;
    }

    private String last;

//    private Context context;
//    private SharedPreferences sp;
    private String phone;
    private String password;
//    private String token;

//    public UserData(/*Context context*/) {
//    }

    public UserData(Context context) {
        load(context);
    }

    private void load(Context context) {
//        this.context = context;
        Log.i(TAG, "load()");
//
        SharedPreferences sp = context.getSharedPreferences("GPSSignInApp", Context.MODE_PRIVATE);
        if (sp == null) {
            Log.e(TAG, "sharedpreference of 'GPSSignInApp' is null");
            return;
        }

        this.name = sp.getString("name", null);
        this.phone = sp.getString("phone", null);
        this.password = sp.getString("password", null);
        this.first = sp.getString("first", "");
        this.last = sp.getString("last", "");
//        this.token = sp.getString("token", null);

        Log.i(TAG, "load() load phone: " + this.phone);
    }

//    public void save(/*String name, String phone, String password, String token*/) {
////        this.name = name;
////        this.phone = phone;
////        this.password = password;
////        this.token = token;
//
//        if (this.sp == null) {
//            this.sp = context.getSharedPreferences("GPSSignInApp", Context.MODE_PRIVATE);
//            if (this.sp == null) {
//                Log.e(TAG, "save() sharedpreference of 'GPSSignInApp' is null");
//                return;
//            }
//        }
//
//        SharedPreferences.Editor editor = sp.edit();
//        editor.putString("name", this.name);
//        editor.putString("phone", this.phone);
//        editor.putString("password", this.password);
//        editor.putString("token", this.token);
//        editor.commit();
//    }
//
    public void save(Context context) {
        SharedPreferences sp = context.getSharedPreferences("GPSSignInApp", Context.MODE_PRIVATE);
        if (sp == null) {
            Log.e(TAG, "sharedpreference of 'GPSSignInApp' is null");
            return;
        }

        SharedPreferences.Editor editor = sp.edit();
        editor.putString("name", this.name);
        editor.putString("phone", this.phone);
        editor.putString("password", this.password);
        editor.putString("first", this.first);
        editor.putString("last", this.last);
        editor.commit(); // should use commit instead of apply
        Log.i(TAG, "save() save phone: " + this.phone + ", services will restart");

        Intent mainService = new Intent(context, MainService.class);
        context.stopService(mainService);
        context.startService(mainService);

        Intent watcherService = new Intent(context, WatcherService.class);
        context.stopService(watcherService);
        context.startService(watcherService);
    }

    public String toString() {
        return "UserData[phone:"+this.phone+",name:"+this.name+",password:"+this.password+",first:"+this.first+",last:"+this.last+"]";
    }
}
